package service

import (
	"gin-vue-admin/global"
	"gin-vue-admin/model"
	"gin-vue-admin/model/request"
)

//@author: [piexlmax](https://github.com/piexlmax)
//@function: CreateBrandCom
//@description: 创建BrandCom记录
//@param: brandCom model.BrandCom
//@return: err error

func CreateBrandCom(brandCom model.BrandCom) (err error) {
	err = global.GVA_DB.Create(&brandCom).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteBrandCom
//@description: 删除BrandCom记录
//@param: brandCom model.BrandCom
//@return: err error

func DeleteBrandCom(brandCom model.BrandCom) (err error) {
	err = global.GVA_DB.Delete(&brandCom).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteBrandComByIds
//@description: 批量删除BrandCom记录
//@param: ids request.IdsReq
//@return: err error

func DeleteBrandComByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]model.BrandCom{},"id in ?",ids.Ids).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: UpdateBrandCom
//@description: 更新BrandCom记录
//@param: brandCom *model.BrandCom
//@return: err error

func UpdateBrandCom(brandCom model.BrandCom) (err error) {
	err = global.GVA_DB.Save(&brandCom).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetBrandCom
//@description: 根据id获取BrandCom记录
//@param: id uint
//@return: err error, brandCom model.BrandCom

func GetBrandCom(id uint) (err error, brandCom model.BrandCom) {
	err = global.GVA_DB.Where("id = ?", id).First(&brandCom).Error
	return
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetBrandComInfoList
//@description: 分页获取BrandCom记录
//@param: info request.BrandComSearch
//@return: err error, list interface{}, total int64

func GetBrandComInfoList(info request.BrandComSearch) (err error, list interface{}, total int64) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&model.BrandCom{})
    var brandComs []model.BrandCom
    // 如果有条件搜索 下方会自动创建搜索语句
    if info.Name != "" {
        db = db.Where("`name` LIKE ?","%"+ info.Name+"%")
    }
    if info.Url != "" {
        db = db.Where("`url` LIKE ?","%"+ info.Url+"%")
    }
	err = db.Count(&total).Error
	err = db.Limit(limit).Offset(offset).Find(&brandComs).Error
	return err, brandComs, total
}